﻿using System;
using System.Collections.Generic;

using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Controls;

using CSLive.ControlPanel.Editor;

namespace CSLive.Controls
{
    public class PresenceSubForm : WrappedSubFormBase, ICSLiveControl
    {
        // storage for the reference to the DropDownList containing car brands
        private YesNoCheckBox YesNo;

        #region Methods

        protected override void OnLoad( EventArgs e )
        {
            if ( !string.IsNullOrEmpty( Context.Request.QueryString[ "Result" ] ) )
            {
                if ( Context.Request.QueryString[ "Result" ].Equals( "Accepted" ) )
                {
                    CSContext.Current.User.SetExtendedAttribute( "LivePresence", Context.Request.QueryString[ "id" ] );
                    Users.UpdateUser( CSContext.Current.User );
                }
                string url = Context.Request.RawUrl;
                if ( url.Contains( "?" ) ) url = url.Remove( url.IndexOf( '?' ) );
                Context.Response.Redirect( url + "?ProfileSaved=true" );
            }
            base.OnLoad( e );
        }

        public override bool IsEnabled()
        {
            return true;
        }

        protected override void AttachChildControls()
        {
            // find the DropDownList that will contain car brands
            YesNo = CSControlUtility.Instance().FindControl( this, YesNoId ) as YesNoCheckBox;

            // if the DropDownList couldn't be found, throw an exception
            if ( YesNo == null )
                throw new InvalidOperationException(
                    "The YesNoId must identify a valid YesNoCheckBox to render a PresenceSubForm" );
        }

        public override void DataBind()
        {
            base.DataBind();

            var user = DataSource as User;
            if ( user != null && !Page.IsPostBack )
                YesNo.Checked = !string.IsNullOrEmpty( user.GetExtendedAttribute( "LivePresence" ) );
        }

        public override void ApplyChangesBeforeCommit( object activeObject )
        {
            var user = activeObject as User;
            if ( YesNo.Checked && user != null )
            {
                if ( string.IsNullOrEmpty( user.GetExtendedAttribute( "LivePresence" ) ) )
                {
                    string url = string.Format( "{0}://{1}{2}", Context.Request.Url.Scheme, Context.Request.Url.Host,
                                                Context.Request.RawUrl );
                    if ( url.Contains( "?" ) ) url = url.Remove( url.IndexOf( '?' ) );
                    var privacyurl = ( string ) LiveConfiguration.Instance.getConfigSetting( Name, "Privacy Url" ).Value;
                    if ( privacyurl.StartsWith( "~" ) || privacyurl.StartsWith( "/" ) )
                    {
                        privacyurl = string.Format( "{0}://{1}{2}", Context.Request.Url.Scheme, Context.Request.Url.Host,
                                                    ResolveClientUrl( privacyurl ) );
                    }
                    Context.Response.Redirect(
                        string.Format(
                            "http://settings.messenger.live.com/applications/websignup.aspx?returnurl={0}&privacyurl={1}",
                            url, privacyurl ) );
                }
            }
            else if ( user != null ) user.SetExtendedAttribute( "LivePresence", "" );
        }

        #endregion

        #region Properties

        // this property will be used to allow the theme developer to provide the ID of the DropDownList containing car brands
        public string YesNoId
        {
            get { return ( string ) ( ViewState[ "YesNoId" ] ?? "" ); }
            set { ViewState[ "YesNoId" ] = value; }
        }

        public Dictionary< string, Control > EditorControls
        {
            get { return new Dictionary< string, Control >(); }
        }

        public string Name
        {
            get { return "Live Presence Profile Form"; }
        }

        public Uri DocsUri
        {
            get
            {
                return
                    new Uri(
                        "http://www.codeplex.com/CSLive/Wiki/View.aspx?title=Live%20Presence&referringTitle=CSLive%202.0" );
            }
        }

        public string Description
        {
            get { return "Windows Live Presence Sub Form"; }
        }

        public string Group
        {
            get { return "Live Messenger"; }
        }

        #endregion
    }
}